<?php

namespace frontend\controllers;

use backend\filter\AccessFilter;
use backend\models\Goods;
use frontend\models\Cart;
use frontend\models\GoodsCategory;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
use yii\helpers\VarDumper;
use yii\web\Cookie;
use frontend\components\SphinxClient;
use yii\sphinx\Query;

class ShopController extends \yii\web\Controller
{
    public $enableCsrfValidation = true;

    public function behaviors()
    {

        return [
            'acf'=>[
                'class'=>AccessControl::className(),
                'only'=>['add-order'],
                'rules'=>[
                    [
                        'allow'=>true,
                        'actions'=>['add-order'],
                        'roles'=>['@'],
                    ],
                    [

                    ],
                ],
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionList($goods_category_id){
        //显示当前点击分类和他的子分类的所有的商品
        //找到当前分类
        $current_cate=GoodsCategory::findOne(['id'=>$goods_category_id]);

        return $this->render('list',['current_cates'=>$current_cate]);

        //VarDumper::dump($allGoods,10,true);
        //查询出属于找到的分类的商品

    }

     public function actionSphinxSearch()
    {
        header('Content-Type: text/html;charset=utf-8');
        $keyword = \Yii::$app->request->get('keyword');
        // $client = \Yii::$app->sphinx;
        return $this->redirect('index');
        $client = new SphinxClient();
        $client->SphinxClient();
        $client->SetServer('127.0.0.1', 9312);
        $client->SetMatchMode(SPH_MATCH_ANY);
        $client->SetLimits(0, 100);
        $result = $client->query('test', '*');
        $result_ids =array_keys($result['matches']);
        $goodsAll =  [];
        foreach ($result_ids as $goods_id) {
            $goodsAll[] = Goods::findOne(['id'=>$goods_id]);
        }

        
        return $this->render('search',['goodsAll'=>$goodsAll]);

        //分词查询搜索
        # code...
    }

    public function actionGoods($goods_id,$goods_category_id){
        //根据商品id获取商品对象的信息
        $goods = Goods::findOne(['id'=>$goods_id]);


        return $this->render('goods',['goods_cate_id'=>$goods_category_id,'goods'=>$goods]);
    }


    /**
     * @return \yii\web\Response
     */
    public function actionNotice(){
        //接收提交的数据
        $data = \Yii::$app->request->post();
        $goods_id = $data['goods_id'];
        $amount = (integer)$data['amount'];

        //判断当前用户是否已经登录
        if(\Yii::$app->user->isGuest){//是游客,没有登录
                \Yii::$app->cartCookie->AddCart($goods_id,$amount)->save();
        }else{ //不是游客,已经登录
                Cart::AddCart($goods_id,$amount)->save();
        }

        //提交后提示保存成功
        //跳转到购物车页面
        return $this->redirect(['shop/cart']);
    }

    /**
     * @return string
     */
    public function actionUpdateCart(){
        $goods_id = \Yii::$app->request->post('goods_id');
        $num = \Yii::$app->request->post('num');
        \Yii::$app->cartCookie->updateCart($goods_id,$num)->save();
        return 'success';
    }

    public function actionCart(){
        //指定视图模版
        $this->layout='base';
        //显示购物车中的商品
        $goods = [];
        $total = 0;
        //用户没有登录
        if(\Yii::$app->user->isGuest){

           $data = \Yii::$app->cartCookie->getCart();

            foreach ($data as  $goods_id=>$amount){
                $goods[$goods_id] = Goods::findOne(['id'=>$goods_id]);
                //把商品的数量添加到商品信息中
                $goods[$goods_id]['amount'] = $amount;
                $goods[$goods_id]['total'] = $amount*($goods[$goods_id]->shop_price);
                $total+=$goods[$goods_id]['total'];

            }

        }else{
            //获取购物车数据表中的商品信息
            $member_id = \Yii::$app->user->id;
            $carts = Cart::find()->where(['member_id'=>$member_id])->all();
            foreach ($carts as $cart){
                $goods_id = $cart->goods_id;
                $amount = $cart->amount;
                $goods[$goods_id] = Goods::findOne(['id'=>$goods_id]);
                $goods[$goods_id]['amount']=$amount;
                $goods[$goods_id]['total']=$amount*$goods[$goods_id]->shop_price;
                $total+=$goods[$goods_id]['total'];
            }
        }

        return $this->render('cart',['goods'=>$goods,'total'=>$total]);
    }

    public function actionDelCart($goods_id=''){
        $goods_id = \Yii::$app->request->post('goods_id');
        //未登录是游客
        if(\Yii::$app->user->isGuest){
            \Yii::$app->cartCookie->DelCartGoods($goods_id)->save();
        }else{
            //登录
            $member_id = \Yii::$app->user->id;
            $cart = Cart::findOne(['member_id'=>$member_id,'goods_id'=>$goods_id]);
            if(!empty($cart)){
                $cart->delete();
            }
        }
        return 'success';
    }



}
